﻿using BCCommon;
using BCData.Sim.Company.Company;
using BCData.Sim.Company.CompanyBusinessType;
using BCDto.Sim.Admin.Admin;
using BCDto.Sim.CompanyBusinessType;
using BCEntity.Sim.Company.CompanyBusinessType;
using BCService.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BCService.Sim.Company.CompanyBusinessType
{
    public class CompanyBusinessTypeService : ICompanyBusinessTypeService
    {
        private readonly ICompanyBusinessTypeData companyBusinessTypeData;
        private readonly ICompanyData companyData;
        public CompanyBusinessTypeService(ICompanyBusinessTypeData companyBusinessTypeData, ICompanyData companyData)
        {
            this.companyBusinessTypeData = companyBusinessTypeData;
            this.companyData = companyData;
        }

        public bool Exists(int companyBusinessTypeId)
        {
            return this.companyBusinessTypeData.Exists(companyBusinessTypeId).GetAwaiter().GetResult();
        }

        public bool Exists(string companyBusinessTypeCode)
        {
            return this.companyBusinessTypeData.Exists(companyBusinessTypeCode.Trim()).GetAwaiter().GetResult();
        }

        public CompanyBusinessTypeDto Add(CompanyBusinessTypeRequestDto requestDto)
        {
            if (this.companyBusinessTypeData.Exists(requestDto.BusinessTypeCode.Trim()).GetAwaiter().GetResult())
            {
                throw new ArgumentException("公司业务类型名称已存在");
            }
            var entity = requestDto.As<CompanyBusinessTypeEntity>();
            return this.companyBusinessTypeData.Add(entity).GetAwaiter().GetResult().As<CompanyBusinessTypeDto>();
        }
        public CompanyBusinessTypeDto Update(CompanyBusinessTypePutDto putDto)
        {
            if (!this.Exists(putDto.CompanyBusinessTypeId))
            {
                throw new ArgumentException("此记录不存在");
            }
            var oldEntity = this.companyBusinessTypeData.GetEntity(putDto.CompanyBusinessTypeId).GetAwaiter().GetResult();
            if (oldEntity.BusinessTypeCode != putDto.BusinessTypeCode)
            {
                if (this.companyBusinessTypeData.Exists(putDto.BusinessTypeCode.Trim()).GetAwaiter().GetResult())
                {
                    throw new ArgumentException("公司业务类型名称已存在");
                }
            }
            var entity = putDto.As<CompanyBusinessTypeEntity>();
            return this.companyBusinessTypeData.Update(entity).GetAwaiter().GetResult().As<CompanyBusinessTypeDto>();
        }

        public bool Delete(int companyBusinessTypeId)
        {
            if (!this.Exists(companyBusinessTypeId))
            {
                throw new ArgumentException("此记录不存在");
            }
            var entity = this.GetEntity(companyBusinessTypeId);
            var companyList = this.companyData.GetList().GetAwaiter().GetResult();
            if(companyList.Any(p=>p.BusinessType == entity.BusinessTypeCode))
            {
                throw new ArgumentException("此公司业务类型存在使用中的公司，请先更改公司业务类型后，再进行删除。");
            }
            return this.companyBusinessTypeData.Delete(companyBusinessTypeId).GetAwaiter().GetResult();
        }

        public CompanyBusinessTypeDto GetEntity(int companyBusinessTypeId)
        {
            return this.companyBusinessTypeData.GetEntity(companyBusinessTypeId).GetAwaiter().GetResult().As<CompanyBusinessTypeDto>();
        }

        public IEnumerable<CompanyBusinessTypeDto> GetList()
        {
            return this.companyBusinessTypeData.GetList().GetAwaiter().GetResult().As<IEnumerable<CompanyBusinessTypeDto>>();
        }

        public PageableList<CompanyBusinessTypeDto> Query(string companyBusinessTypeName, DateTime? start, DateTime? end, int pageIndex, int pageSize)
        {
            var dataResult = this.companyBusinessTypeData.Query(companyBusinessTypeName, start, end, pageIndex, pageSize).GetAwaiter().GetResult();

            return new PageableList<CompanyBusinessTypeDto>
            {
                Count = dataResult.Item2,
                Items = dataResult.Item1.As<IEnumerable<CompanyBusinessTypeDto>>(),
                PageIndex = pageIndex,
                PageSize = pageSize,
                ServerTime = DateTime.Now
            };
        }
    }
}
